package com.jogern.baseadapter.recycler;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * RecyclerView通用的Adapter
 * Created by jogern on 2016/9/8 0008.
 */

public abstract class BaseRecyclerAdapter<T> extends
        RecyclerView.Adapter<RecyclerTypeHolder.ViewHolder> {
    private SparseArrayCompat<ItemType<T>> mItemTypes = new SparseArrayCompat<>();
    protected RecyclerView mRecyclerView;
    protected List<T> mList;

    public BaseRecyclerAdapter(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
    }

    public List<T> getList() {
        return mList;
    }

    /**
     * 增加Item的类型
     * @param viewType 类型
     * @param itemType item的Holder的代理创建
     */
    protected void addItemType(int viewType, ItemType<T> itemType) {
        if (mItemTypes.get(viewType) != null) {
            throw new IllegalArgumentException("this type is exist");
        }
        mItemTypes.put(viewType, itemType);
    }

    private int findItemType(T t) {
        for (int i = mItemTypes.size() - 1; i >= 0; i--) {
            ItemType<T> tItemType = mItemTypes.valueAt(i);
            if (tItemType.isViewType(t)) {
                return mItemTypes.keyAt(i);
            }
        }
        throw new IllegalArgumentException("can not find the type");
    }


    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public final int getItemViewType(int position) {
        return findItemType(mList.get(position));
    }

    @Override
    public final RecyclerTypeHolder.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemType<T> tItemType = mItemTypes.get(viewType);
        RecyclerTypeHolder<T> tTypeHolder = tItemType.newHolder(mRecyclerView, this);
        View inflate = View.inflate(parent.getContext(), tTypeHolder.getLayoutId(), null);
        return tTypeHolder.createHolder(inflate);
    }

    public RecyclerTypeHolder getRecyclerTypeHolder(int position) {
        RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(position);
        if (holder != null && holder instanceof RecyclerTypeHolder.ViewHolder) {
            RecyclerTypeHolder.ViewHolder viewHolder = (RecyclerTypeHolder.ViewHolder) holder;
            return viewHolder.getTypeHolder();
        }
        return null;
    }

    @Override
    public final void onBindViewHolder(RecyclerTypeHolder.ViewHolder holder, int position) {
        holder.onBindHolder(holder, position);
    }

    @Override
    public final void onBindViewHolder(RecyclerTypeHolder.ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public interface ItemType<T> {

        boolean isViewType(T t);

        RecyclerTypeHolder<T> newHolder(RecyclerView recyclerView, BaseRecyclerAdapter<T> adapter);
    }
}
